package cn.com.zs.android.view;

import android.content.Context;
import android.graphics.Canvas;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import cn.com.zs.android.model.GameParams;
import cn.com.zs.android.util.Constants;
import cn.com.zs.android.util.GameUtil;
import cn.com.zs.android.util.HolePlace;
import cn.com.zs.android.util.Image;

/**
 * 绘画游戏背景
 * @author 曾帅
 */
public class DrawBackground extends View {

	DrawImageManager drawImageManager = new DrawImageManager();
	Handler handler = new BackGroundHandler();
	Runnable backGroundRun = new BackGroundRun();
	Image image;
	
	public DrawBackground(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		
		this.setKeepScreenOn(true);	// 保持屏幕常亮
		this.reStartGame();
		
		Thread backGroundThread = new Thread(backGroundRun);
		backGroundThread.start();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
//		Log.i(this.toString(), "创建游戏背景" + GameParams.seq);
		if(image == null) {
			image = new Image(canvas, DrawBackground.this.getResources());
		}
		// 通过绘图次数计算出小鱼当前应当出现的状态
//		int stauts = (GameParams.seq - 1) % Constants.EACH_GROUP_IMAGE_NUMBER + 1;
		// 绘图
		long readyTime = GameUtil.getReadyTime();
		long gameTime = -1L;
		if(readyTime > 0) {
			GameParams.setBackgrouudAlpha_150();
			drawImageManager.backgroundImage(image);
			gameTime = GameParams.gameCountdownTime / 1000;
			if (readyTime < GameParams.startCountdownTime / 1000) {
				drawImageManager.countdownImage(image, readyTime);
			}
		} else {
			gameTime = GameUtil.getGameTime();
			if (gameTime > 0) {
				GameParams.setBackgrouudAlpha_255();
			} else {
				GameParams.setBackgrouudAlpha_150();
			}
			drawImageManager.draw(image, GameParams.isResetDate);
			if (gameTime <= 0) {
				GameParams.isResetDate = false;
				drawImageManager.drawGameOver(image);
				drawImageManager.drawReStart(image);
				GameParams.isNeedRestartGame = true;
			}
		}
		// 创建时间和分数
		drawImageManager.drawTimeAndScore(canvas, gameTime);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		if(event.getAction() != MotionEvent.ACTION_DOWN){
			return true;
		}
		float x = event.getX();
		float y = event.getY();
		// 设置音效
		drawImageManager.voiceSettings(this.getContext(), x, y);
		
		if(GameParams.isNeedRestartGame) {
			int[] holePlace = HolePlace.getDesignationPlace(Constants.GAME_RESTART_STATUS);
			if(x > holePlace[0] && x < holePlace[2] && y > holePlace[1] && y < holePlace[3]){
				this.reStartGame();
			}
		} else {
			drawImageManager.touch(this.getContext(), x, y);
		}
		
		return super.onTouchEvent(event);
	}
	
	class BackGroundHandler extends Handler {
		
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			DrawBackground.this.postInvalidate();
			GameParams.seq ++;
			handler.postDelayed(backGroundRun, GameParams.intervalTime);
		}
		
	}
	
	class BackGroundRun implements Runnable {
		
		@Override
		public void run() {
			handler.handleMessage(new Message());
		}
	}
	
	private void reStartGame() {
		GameParams.isResetDate = true;
		GameParams.isNeedRestartGame = false;
		GameParams.resetGameTime();
		drawImageManager.initDate();
	}
	
}
